package oboe.controllers.jml.helpers;

import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;

import oboe.controllers.jml.JMLController;
import oboe.core.Oboe;
import oboe.utils.CallbackHandle;

class JMLPopUp extends JMLNode
{
private static var activePopUp:JMLPopUp;

private var asset:Sprite;
private var cbh:CallbackHandle;

public function new(head:String, jml:Object, parent:JMLController)
{
	super(head, jml, parent);
	this.asset = this.getDisplayObject() as Sprite;
}

public function slot_showPopUp( id:String ):Void
{
	if( id != this.asset.name || activePopUp == this ) return;
	if( activePopUp && activePopUp != this )
	{
	activePopUp.message("hidePopUp");
	}
	activePopUp = this;
	this.asset.visible = true;
	this.cbh = Object(Oboe.stage).addCallbackEx( MouseEvent.CLICK, onClick );
	this.cbh.ignoreCount = 1;
}

private function onClick( e:Event ):Void
{
	var p:DisplayObject = e.target as DisplayObject;
	while( p && p != this.asset )
	{
	p = p.parent;
	}
	
	if( !p ) this.slot_hidePopUp();
}

protected override function createDisplayObject(type:String):DisplayObject
{
	var s:Sprite = new Sprite();
	s.visible = false;
	return s;
}

public function slot_hidePopUp():Void
{
	this.getDisplayObject().visible = false;
	if( activePopUp == this )
	{
	activePopUp = null;
	Object(Oboe.stage).removeCallback( this.cbh );
	this.cbh = null;
	}
}
}